package com.gitee.sop.layui.adminserver.module.background.develop.rest;

import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysUser;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysDataModifyLockService;
import com.gitee.sop.layui.adminserver.module.background.develop.dto.SysFunctionRoleDTO;
import com.gitee.sop.layui.adminserver.module.background.develop.entity.SysFunctionRole;
import com.gitee.sop.layui.adminserver.module.background.develop.service.SysFunctionRoleService;
import com.gitee.sop.layui.adminserver.module.background.develop.util.SysFunctionRoleUtil;
import com.gitee.sop.layui.adminserver.module.base.bo.ValidGroup;
import com.gitee.sop.layui.adminserver.module.base.bo.ZTreeNode;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.zch.admin.base.entity.SysAdminFunctionRole模型http接口处理
 *
 * @author: admin
 * @date: 2019-11-27
 * @version: 1.0.0
 */
@Validated(value = {ValidGroup.All.class})
@RestController
@RequestMapping(value = "/develop/functionRole", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SysFunctionRoleRestController {

    @Lazy
    @Resource
    private SysFunctionRoleService baseService;
    @Lazy
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;

    @PostMapping(value = {"/add"})
    public Result add(SysFunctionRoleDTO query) {
        SysFunctionRole sysFunctionRole = new SysFunctionRole();
        ObjectUtil.switchAllFieldValue(sysFunctionRole, query);
        baseService.add(sysFunctionRole);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"})
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        sysDataModifyLockService.checkLocked(SysFunctionRoleUtil.dataLockKey(id));

        baseService.deleteSelfAndChildrenById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"})
    public Result updateById(@PathVariable(value = "id") Integer id, SysFunctionRoleDTO query) {
        sysDataModifyLockService.checkLocked(SysFunctionRoleUtil.dataLockKey(id));

        SysFunctionRole sysFunctionRole = new SysFunctionRole();
        ObjectUtil.switchAllFieldValue(sysFunctionRole, query);

        baseService.updateById(sysFunctionRole);
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"})
    public Result searchAll() {
        List<SysFunctionRole> list = baseService.getAllByUserUuid(ServiceContext.me().checkedUserAndGet(SysUser.class).getUuid());
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/id/{id}"})
    public Result searchOneById(@PathVariable(value = "id") Integer id) {
        SysFunctionRole SysAdminFunctionRole = baseService.getById(id);
        return Result.success(SysAdminFunctionRole);
    }

    @RequestMapping(value = {"/search/tree/{userUuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchTree(@PathVariable String userUuid) {
        List<ZTreeNode> list = baseService.searchTreeByUserUuid(userUuid);
        return ObjectUtil.toJson(list);
    }

}
